package com.hexus.eve.actions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

import com.hexus.eve.entities.Category;
import com.hexus.eve.utils.TranslationUtils;

public class Categories extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private List<Category> categories= new ArrayList<Category>();
	@Autowired
	private TranslationUtils translationUtils;
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public String execute() {
		Session session = sessionFactory.openSession();
		try {
			categories = session.createQuery("from Category where published=1 order by name").list();
			translationUtils.translate(categories, getLocale());
			Collections.sort(categories);
		}
		catch(Exception ex) {
			Logger logger = Logger.getLogger(this.getClass().getName());
			logger.error(ex.getMessage());
			ex.printStackTrace();
		}
		finally {
			session.close();
		}
		return SUCCESS;
	}

	public List<Category> getCategories() {
		return categories;
	}
	

}
